// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

  

    @property
    propagate:boolean=false;

   //是否按下键
    _down :boolean=false;
    // LIFE-CYCLE CALLBACKS:

    protected onLoad () {

        this._down=false;

        this.node.opacity=100;

        //按下鼠标事件
        this.node.on(cc.Node.EventType.MOUSE_DOWN,function(event){

            cc.log('Drag started...');

            this.node.opacity=255;

            //按下键为真
            this._down=true;

            if(!this.propagate){
                event.stopPropagation();
                
            }
        },this);

        //移动鼠标事件
        this.node.on(cc.Node.EventType.MOUSE_MOVE,function(event){

            //按下键，则返回
            if(!this._down){
                event.stopPropagation();
                return;
            }

            this.node.opacity=255;

            let delta=event.getDelta();

            this.node.x+=delta.x;

            this.node.y+=delta.y;
            
            //如果事件不向上传播
            if(!this.propagate){
                event.stopPropagation();
            }
        },this);

        //鼠标离开事件
        this.node.on(cc.Node.EventType.MOUSE_LEAVE, function () {
            if (!this._down) {
                event.stopPropagation();
                return
            }
            this.node.opacity = 160;
            cc.log('Drag leave ...');
            this._down = false;
        }, this);

        //鼠标抬起事件
        this.node.on(cc.Node.EventType.MOUSE_UP, function () {
            
            if (!this._down) {
                event.stopPropagation();
                return
            }
            cc.log('Drag done ...');
            this.node.opacity = 160;
            this._down = false;
        }, this);

        //监听自定义事件
        this.node.on('say-hello', function (event) {

            console.log("我开始接收事件了："+event.detail.msg);

          });

    }
     protected  start () {

        //发射自定义事件
        this.node.emit('say-hello', {
          msg: 'Hello, this is Cocos Creator,我开始发送自定义事件了',
        });
      }
    // update (dt) {}
}
